// pages/index/reservation /reserve/reserve.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    L:'',
    R:'active',
    count:1,
    pick1: '../../../../imgs/pick1.png',
    pick2: '../../../../imgs/pick2.png',
    id1: 'active1',
    id2: 'active2',
    sty:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  console.log(options)
  let _this = this;
  let mobile;
  if (options.mobile) {
    mobile = options.mobile
  } else {
    mobile = ''
  }
  console.log('options',options)
  console.log(wx.getStorageSync('shopname'))
  if(options.coupon==0){
    console.log(111111111111)
    _this.setData({
      date: options,
      price2: options.price,
      order_price: options.price,
      appoint_date: options.year + '-' + options.day2,
      mobile:mobile,
      shopname: wx.getStorageSync('shopname')
    })
  }else if (options.coupon==1){
    console.log(2222222)
    let date = JSON.parse(options.date);
    // console.log('1111',options)
    // console.log('222',date)
    let order_price = date.price - options.amount;
    if (order_price <0){
      order_price = 0
    }
    console.log(order_price)
    // let reg = /\d+(\.\d{1,2})?/g;
    // order_price = reg.exec(order_price)[0];
    _this.setData({
      date: date,
      price2: options.price,
      order_price: order_price,
      count:options.count,
      mobile: options.mobile,
      appoint_date: options.appoint_date,
      coupon:options.coupon,
      coupon_name: options.coupon_name,
      amount: options.amount,
      coupon_id:options.coupon_id,
      coupon: options.coupon,
      shopname: wx.getStorageSync('shopname')
    })
  }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  addOrReduce(e){
    // console.log(e)
    var id = e.currentTarget.id;
    var that = this;
    var count = parseInt(that.data.count);
    var num = parseInt(that.data.date.num.slice(-1));
    // console.log(num)
    
    if(id=='L'){
      if (count==1){
        count = 1
      }else {
       count = count - 1
      }
      // console.log(count)
      that.setData({
        L:'active',
        R:'',
        count: count,
        // allprice:that.data.date.price*count
      })
    }else {
      if(count < num){
        count= count + 1
      }else {
        count=count
      }
      // console.log(count)
      that.setData({
        R:'active',
        L:'',
        count: count,
        // allprice: that.data.date.price * count
      })
    }
  },
  pay(e){
    let _this = this;
    if (!_this.data.mobile){
      if (!_this.data.tel) {
        wx.showToast({
          title: '请输入手机号',
        })
        return
      }
    }
  
    app.wxrequest({
      url: app.globalData.baseUrl + '/api/house/appoint',
      data: {
        session_key: app.gs(),
        house_cate_id: _this.data.date.house_cate_id,
        appoint_date: _this.data.appoint_date,
        tel: _this.data.mobile,
        hour: _this.data.date.long,
        begin_time: _this.data.date.begin_time,
        start_time: _this.data.date.hour,
        end_time: _this.data.date.hou,
        order_price: _this.data.date.price,
        coupons_id: _this.data.coupon_id||'',
        coupons_price: _this.data.amount||''
      },
      success:function(res){
        wx.hideLoading();
        console.log(res)
        
        app.wxrequest({
          url: app.globalData.baseUrl + '/api/pay/payByOrder',
          data: {
            session_key: app.gs(),
            order_id: res.data.data,
            type: _this.data.sty,
            form_id: e.detail.formId
          },
          success: function (res) {
            wx.hideLoading()
            console.log('resresresres',res);
            // return
            if (res.data.message=='支付成功'){
                wx.showToast({
                  title: '支付成功',
                });
                setTimeout(function(){
                  wx.redirectTo({
                    url: '../reservation',
                  })
                },2000)
            }
            if (_this.data.sty == 1) {
              wx.requestPayment({
                timeStamp: res.data.data.timeStamp,
                nonceStr: res.data.data.nonceStr,
                package: res.data.data.package,
                signType: res.data.data.signType,
                paySign: res.data.data.paySign,
                success(res) {
                  console.log('resresresresresresresresresresresresresres',res)
                  if (res.errMsg == "requestPayment:ok") {
                   wx.showToast({
                     title: '支付成功',
                   });
                   setTimeout(function(){
                      wx.redirectTo({
                        url: '../reservation',
                      })
                   },2000)
                  } else {
                    wx.showToast({
                      title: '支付失败',
                    })
                  }
                }
              })
            } else if (_this.data.sty == 2) {
              if (res.data.message == 402) {
                wx.showToast({
                  title: '会员卡余额不足',
                })
              } else if (res.data.code==400){
                wx.showToast({
                  title: '支付失败',
                })
              } else if (res.data.message == '支付成功') {
               
                  if(_this.data.coupon==1){
                    wx.showToast({
                      title: '支付成功',
                      success(res) {
                        setTimeout(function () {
                          wx.redirectTo({
                            url:'../reservation'
                          })
                        }, 2000)
                      }
                    })
                  }else {
                    wx.showToast({
                      title: '支付成功',
                      success(res) {
                        setTimeout(function () {
                          wx.navigateBack({
                            Number: 1
                          })
                        }, 2000)
                      }
                    })
                  }
               
                
              
              }
            }



          }
        })

       
       
      }
   })
  },
  tel(e){
    this.setData({
      mobile:e.detail.value
    })
  },
  payStyle(e) {
    // console.log(e)
    let _this = this;
    if (e.currentTarget.id == 'active2') {
      _this.setData({
        pick2: '../../../../imgs/pick1.png',
        pick1: '../../../../imgs/pick2.png',
        sty: e.currentTarget.dataset.sty
      })
    } else {
      _this.setData({
        pick1: '../../../../imgs/pick1.png',
        pick2: '../../../../imgs/pick2.png',
        sty: e.currentTarget.dataset.sty
      })
    }
  },
  chooseCoupon(e){
    let _this = this;
    let date = JSON.stringify(_this.data.date);
    wx.navigateTo({
      url: '../../wineShop/orderPay/chooseCoupon/chooseCoupon?sty=' + e.currentTarget.dataset.sty + '&date=' + date + '&count=' + _this.data.count + '&order_price=' + _this.data.order_price + '&mobile=' + _this.data.mobile + '&appoint_date=' + _this.data.appoint_date + '&shopname=' + _this.data.shopname,
    })
  },
  // formsubmit(e){
  //   // console.log(e,'wwwwwwwwww')
  //   this.setData({
  //     formid: e.detail.formId 
  //   })
  // }
})